#pragma once

#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>

// 异步写日志的日志的队列
template <class T>
class LockQueue
{
public:
    // 多个worker线程会写日志
    void Push(const T& data)
    {
        std::lock_guard<std::mutex> lock(m_mutex);
        m_queue.push(data);
        m_condvariable.notify_one();
    }

    void Pop(T* data)
    {
        std::unique_lock<std::mutex> lock(m_mutex);
        while(m_queue.empty())
        {
            // 日志队列为空，写日志线程进入wait状态
            m_condvariable.wait(lock);
        }

        *data = m_queue.front();
        m_queue.pop();
    }

private:
    std::queue<T> m_queue;
    std::mutex m_mutex;
    std::condition_variable m_condvariable;
};